package cn.shopifymall.hw_od;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @Author 孤风雪影
 * @Email gitee.com/efairy520
 * @Date 2023/3/14 22:34
 * @Version 1.0
 */
public class HJ08 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String sizeString = scanner.nextLine();
        int max = Integer.parseInt(sizeString);

        String dataString = scanner.nextLine();
        String[] split = dataString.split(" ");
        List<Integer> list = Arrays.stream(split).filter(s -> !"".equals(s)).map(Integer::parseInt).collect(Collectors.toList());

        int result = 0;
        for (int i = 0; i < list.size(); i++) {
            int range = list.get(i);
            for (int j = i + 1; j < list.size(); j++) {
                if (range + list.get(j) <= max) {
                    range += list.get(j);
                } else {
                    break;
                }
            }
            if (range > result) {
                result = range;
            }
        }

        System.out.println(result);
    }
}

